package com.mall.shopping.services.carts;

import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.CartItemConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dto.*;
import org.apache.dubbo.config.annotation.Service;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * \* Author: Valley
 * \* Date: 2022/1/19
 * \* Time: 20:23
 * \* Description:
 */
@Service
public class ICartServiceImpl implements ICartService {


    @Autowired
    ItemMapper itemMapper;
    @Autowired
    RedissonClient redissonClient;

    /**
     * @description: 获取用户购物车列表
     * @param: [request]
     * @return: com.mall.shopping.dto.CartListByIdResponse
     * @author 王力
     * @date: 2022-01-19 21:20:51
     */
    @Override
    public CartListByIdResponse getCartListById(CartListByIdRequest request) {
        try {
            CartListByIdResponse listByIdResponse = new CartListByIdResponse();
            List<CartProductDto> productDtos = new ArrayList<>();
            Long userId = request.getUserId();
            RMap<Long, CartProductDto> map = redissonClient.getMap(userId + "");
            Set<Long> longs = map.keySet();
            for (Long key : longs) {
                CartProductDto cartProductDto = map.get(key);
                productDtos.add(cartProductDto);
            }
            listByIdResponse.setCartProductDtos(productDtos);
            listByIdResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            listByIdResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            return listByIdResponse;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;

    }


    /**
     * @description: 添加到购物车
     * @param: [request]
     * @return: com.mall.shopping.dto.AddCartResponse
     * @author 王力
     * @date: 2022-01-19 23:18:39
     */
    @Override
    public AddCartResponse addToCart(AddCartRequest request) {
        try {
            AddCartResponse cartResponse = new AddCartResponse();
            RMap<Object, Object> cart = redissonClient.getMap("" + request.getUserId());
            Item item = itemMapper.selectByPrimaryKey(request.getItemId());
            item.setNum(request.getNum());
            CartProductDto productDto = CartItemConverter.item2Dto(item);
            productDto.setChecked("false");
            productDto.setProductNum((long)request.getNum());
            CartProductDto cartProductDto = (CartProductDto) cart.get(request.getItemId());
            if (cartProductDto == null || cartProductDto.getProductNum() == null){
                cart.put(request.getItemId(), productDto);
            }else {
                cartProductDto.setProductNum(cartProductDto.getProductNum()+request.getNum());
                cart.put(request.getItemId(), cartProductDto);
            }
            cartResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            cartResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            return cartResponse;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;

    }

    /**
     * @description: 更新购物车商品数量
     * @param: [request]
     * @return: com.mall.shopping.dto.UpdateCartNumResponse
     * @author 王力
     * @date: 2022-01-20 10:23:25
     */
    @Override
    public UpdateCartNumResponse updateCartNum(UpdateCartNumRequest request) {
        try {
            UpdateCartNumResponse response = new UpdateCartNumResponse();
            RMap<Object, Object> map = redissonClient.getMap("" + request.getUserId());
            CartProductDto cartProductDto = (CartProductDto) map.get(request.getItemId());
            cartProductDto.setChecked(request.getChecked());
            cartProductDto.setProductNum((long) request.getNum());
            map.put(request.getItemId(), cartProductDto);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            return response;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    /**
     * @description: 全选购物车中的商品
     * @param:
     * @return:
     * @author wangzhuo
     * @date: 2022-01-19 20:04
     */
    public CheckAllItemResponse checkAllCartItem(CheckAllItemRequest request) {
        CheckAllItemResponse response = new CheckAllItemResponse();
        RMap<Long, CartProductDto> map = redissonClient.getMap(request.getUserId() + "");
        Set<Long> longs = map.keySet();
        for (Long key : longs) {
            CartProductDto cartProductDto = map.get(key);
            cartProductDto.setChecked(request.getChecked());
            map.put(cartProductDto.getProductId(), cartProductDto);
        }

        response.setCode("000000");
        response.setMsg("成功");
        return response;
    }

    @Override
    /**
     * @description: 删除购物车中的商品
     * @param:
     * @return:
     * @author wangzhuo
     * @date: 2022-01-19 20:04
     */
    public DeleteCartItemResponse deleteCartItem(DeleteCartItemRequest request) {
        DeleteCartItemResponse response = new DeleteCartItemResponse();
        RMap<Object, Object> map = redissonClient.getMap(request.getUserId() + "");
        map.remove(request.getItemId());
        response.setCode("000000");
        response.setMsg("成功");
        return response;
    }

    @Override
    /**
     * @description: 删除购物车中选中的商品
     * @param:
     * @return:
     * @author wangzhuo
     * @date: 2022-01-19 20:04
     */
    public DeleteCheckedItemResposne deleteCheckedItem(DeleteCheckedItemRequest request) {
        DeleteCheckedItemResposne response = new DeleteCheckedItemResposne();

        RMap<Long, CartProductDto> map = redissonClient.getMap(request.getUserId() + "");
        Set<Long> longs = map.keySet();
        for (Long key : longs) {
            CartProductDto productDto = map.get(key);
            if (productDto.getChecked().equals("true")) {
                map.remove(key);
            }
        }
        response.setCode("000000");
        response.setMsg("成功");
        return response;
    }

    @Override
    public ClearCartItemResponse clearCartItemByUserID(ClearCartItemRequest request) {
        return null;
    }
}
